{% extends "admin/layout.html" %}

{% block content %}
<div class="container-fluid">
    <div class="row justify-content-center">
        <div class="col-md-8 col-lg-6">
            <div class="card">
                <div class="card-header bg-white">
                    <h4 class="mb-0">
                        <i class="fas fa-user-plus me-2"></i>{{ title }}
                    </h4>
                </div>
                <div class="card-body">
                    <form method="POST">
                        {{ form.hidden_tag() }}

                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label class="form-label">用户名 <span class="text-danger">*</span></label>
                                    {{ form.username(class="form-control", placeholder="请输入用户名") }}
                                    {% for error in form.username.errors %}
                                    <div class="text-danger small">{{ error }}</div>
                                    {% endfor %}
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label class="form-label">真实姓名</label>
                                    {{ form.real_name(class="form-control", placeholder="请输入真实姓名") }}
                                    {% for error in form.real_name.errors %}
                                    <div class="text-danger small">{{ error }}</div>
                                    {% endfor %}
                                </div>
                            </div>
                        </div>



                        <div class="mb-3">
                            <label class="form-label">密码 {% if not user %}<span class="text-danger">*</span>{% endif %}</label>
                            {{ form.password_hash(class="form-control", placeholder="请输入密码", type="password") }}
                            {% for error in form.password_hash.errors %}
                            <div class="text-danger small">{{ error }}</div>
                            {% endfor %}
                            <div class="form-text">
                                {% if user %}
                                如果不修改密码，请留空
                                {% else %}
                                请输入用户密码
                                {% endif %}
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-12">
                                <div class="mb-3">
                                    <label class="form-label">管理员权限</label>
                                    <div class="form-check form-switch mt-2">
                                        {{ form.is_super(class="form-check-input") }}
                                        <label class="form-check-label">超级管理员</label>
                                    </div>
                                    {% for error in form.is_super.errors %}
                                    <div class="text-danger small">{{ error }}</div>
                                    {% endfor %}
                                </div>
                            </div>
                        </div>

                        <div class="d-grid gap-2 d-md-flex justify-content-md-end mt-4">
                            <a href="{{ url_for('admin.admin_user.user_management') }}" class="btn btn-secondary me-md-2">
                                <i class="fas fa-arrow-left me-1"></i> 返回列表
                            </a>
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save me-1"></i> 保存用户
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // member_id 字段已移除
        // 表单验证
        const form = document.querySelector('form');
        if (form) {
            form.addEventListener('submit', function(e) {
                const username = document.getElementById('username');
                const password = document.getElementById('password');

                // 验证用户名
                if (!username.value.trim()) {
                    e.preventDefault();
                    alert('用户名不能为空');
                    username.focus();
                    return false;
                }

                // 如果是添加用户，验证密码
                {% if not user %}
                if (!password.value.trim()) {
                    e.preventDefault();
                    alert('密码不能为空');
                    password.focus();
                    return false;
                }
                {% endif %}
            });
        }
    });
</script>
{% endblock %}